package org.jeecg.modules.ec.service.impl;

import org.jeecg.modules.ec.entity.EcArchiveInfo;
import org.jeecg.modules.ec.entity.EcEmergencyContact;
import org.jeecg.modules.ec.mapper.EcEmergencyContactMapper;
import org.jeecg.modules.ec.mapper.EcArchiveInfoMapper;
import org.jeecg.modules.ec.service.IEcArchiveInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 档案信息管理
 * @Author: jeecg-boot
 * @Date:   2025-09-02
 * @Version: V1.0
 */
@Service
public class EcArchiveInfoServiceImpl extends ServiceImpl<EcArchiveInfoMapper, EcArchiveInfo> implements IEcArchiveInfoService {

	@Autowired
	private EcArchiveInfoMapper ecArchiveInfoMapper;
	@Autowired
	private EcEmergencyContactMapper ecEmergencyContactMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(EcArchiveInfo ecArchiveInfo, List<EcEmergencyContact> ecEmergencyContactList) {
		ecArchiveInfoMapper.insert(ecArchiveInfo);
		if(ecEmergencyContactList!=null && ecEmergencyContactList.size()>0) {
			for(EcEmergencyContact entity:ecEmergencyContactList) {
				//外键设置
				entity.setServiceProviderId(ecArchiveInfo.getId());
				ecEmergencyContactMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(EcArchiveInfo ecArchiveInfo,List<EcEmergencyContact> ecEmergencyContactList) {
		ecArchiveInfoMapper.updateById(ecArchiveInfo);
		
		//1.先删除子表数据
		ecEmergencyContactMapper.deleteByMainId(ecArchiveInfo.getId());
		
		//2.子表数据重新插入
		if(ecEmergencyContactList!=null && ecEmergencyContactList.size()>0) {
			for(EcEmergencyContact entity:ecEmergencyContactList) {
				//外键设置
				entity.setServiceProviderId(ecArchiveInfo.getId());
				ecEmergencyContactMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		ecEmergencyContactMapper.deleteByMainId(id);
		ecArchiveInfoMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			ecEmergencyContactMapper.deleteByMainId(id.toString());
			ecArchiveInfoMapper.deleteById(id);
		}
	}
	
}
